package com.it.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.it.entity.Inventory;
import com.it.entity.InventoryInfo;
import com.it.service.InventoryService;
import com.it.util.DateUtil;
import com.it.util.ItdragonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *
 */
@Service
public class InventoryServiceImpl implements InventoryService {

    @Resource
    private ItdragonUtils itdragonUtils;
    @Resource
    private com.it.mapper.InventoryMapper InventoryMapper;
    @Resource
    private com.it.mapper.InventoryInfoMapper InventoryInfoMapper;

    @Override
    public Page<Inventory> selectPage(Inventory entity, int page, int limit) {
        EntityWrapper<Inventory> searchInfo = new EntityWrapper<>();
        Page<Inventory> pageInfo = new Page<>(page, limit);
        if (ItdragonUtils.stringIsNotBlack(entity.getProductUuId())) {
            searchInfo.eq("productUuId", entity.getProductUuId());
        }
        List<Inventory> resultList = InventoryMapper.selectPage(pageInfo, searchInfo);
        if (!resultList.isEmpty()) {
            pageInfo.setRecords(resultList);
        }
        return pageInfo;
    }

    @Override
    public Page<InventoryInfo> selectPage(InventoryInfo entity, int page, int limit) {
        EntityWrapper<InventoryInfo> searchInfo = new EntityWrapper<>();
        Page<InventoryInfo> pageInfo = new Page<>(page, limit);
        if (ItdragonUtils.stringIsNotBlack(entity.getProductUuId())) {
            searchInfo.like("productUuId", entity.getProductUuId());
        }
        if (ItdragonUtils.stringIsNotBlack(entity.getUserName())) {
            searchInfo.like("userName", entity.getUserName());
        }
        if (ItdragonUtils.stringIsNotBlack(entity.getProductName())) {
            searchInfo.like("productName", entity.getProductName());
        }
        searchInfo.orderBy("time desc");
        List<InventoryInfo> resultList = InventoryInfoMapper.selectPage(pageInfo, searchInfo);
        if (!resultList.isEmpty()) {
            pageInfo.setRecords(resultList);
        }
        return pageInfo;
    }

    @Override
    public boolean insert(Inventory entity) {
        Integer insert = InventoryMapper.insert(entity);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insert(InventoryInfo entity) {
        entity.setTime(DateUtil.getNowDateSS());
        entity.setUserName(itdragonUtils.getSessionUserName());
        Integer insert = InventoryInfoMapper.insert(entity);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean editById(Inventory entity) {
        Integer insert = InventoryMapper.updateById(entity);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(String ids) {
        String[] idList = ids.split(",");
        int result = 0;
        for (String s : idList) {
            result = InventoryMapper.deleteById(s);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteInfoById(String ids) {
        String[] idList = ids.split(",");
        int result = 0;
        for (String s : idList) {
            result = InventoryInfoMapper.deleteById(s);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public List<Inventory> getList(Inventory entity) {
        EntityWrapper<Inventory> wrapper = new EntityWrapper<>();
        if (ItdragonUtils.stringIsNotBlack(entity.getProductUuId())) {
            wrapper.eq("productUuId", entity.getProductUuId());
        }

        List<Inventory> resultList = InventoryMapper.selectList(wrapper);
        return resultList;
    }

    @Override
    public Inventory getOne(String id) {
        Inventory Inventory = InventoryMapper.selectById(id);
        if (Inventory != null) {
            return Inventory;
        } else {
            return new Inventory();
        }
    }

    @Override
    public Inventory checkHave(String productId) {
        EntityWrapper<Inventory> wrapper = new EntityWrapper<>();
        if (ItdragonUtils.stringIsNotBlack(productId)) {
            wrapper.eq("productId", productId);
        }
        List<Inventory> resultList = InventoryMapper.selectList(wrapper);
        if (resultList.isEmpty()) {
            return null;
        }
        return resultList.get(0);
    }

    @Override
    public boolean editStock(String productId, Integer num) {
        //查出药品对应的库存信息
        EntityWrapper<Inventory> wrapper = new EntityWrapper<>();
        wrapper.eq("productId", productId);
        List<Inventory> resultList = InventoryMapper.selectList(wrapper);
        if (!resultList.isEmpty()) {
            Inventory inventory = resultList.get(0);
            inventory.setStock(inventory.getStock() - num);
            inventory.setSale(inventory.getSale() + num);
            Integer insert = InventoryMapper.updateById(inventory);
            if (insert > 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public Float payPrice() {
        EntityWrapper<InventoryInfo> wrapper = new EntityWrapper<>();
        List<InventoryInfo> inventoryInfoList = InventoryInfoMapper.selectList(wrapper);
        Float price = Float.valueOf(0);
        for (InventoryInfo inventoryInfo : inventoryInfoList) {
            price += inventoryInfo.getCostPrice() * inventoryInfo.getStock();
        }
        return price;
    }
}